﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace FizzBuzz
{
    internal class EventDemo : IDisposable
    {
//        private readonly ManualResetEvent m_event;
        private AutoResetEvent m_event;

        public EventDemo()
        {
//            m_event = new ManualResetEvent(false); // create unsignaled
            m_event = new AutoResetEvent(false);

            Thread thread = new Thread(DoWork);
            thread.Start();
        }

        public void DoWork()
        {
            int counter = 0;
            while (true)
            {
                m_event.WaitOne();
                counter++;
                Console.WriteLine("Iteration:{0}", counter);
            }
        }

        public void GoThread()
        {
            m_event.Set();
            Console.WriteLine("Go Thread");
        }

        public void StopThread()
        {
            m_event.Reset();
            Console.WriteLine("StopThread");
        }

        public void Dispose()
        {
            m_event.Close();
        }
    }
}